﻿using ReflectionStudio.Core.Events;
using System;
using System.Collections.Generic;
using System.Data;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Input;
using WPFExcelReport.Helpers;
namespace WPFExcelReport
{
    /// <summary>
    /// 专用于布卷剪布
    /// </summary>
    public partial class UIFQI03 : UIRQI03
    {
        #region ==CQTY===========
        public static readonly DependencyProperty CQTYProperty = DependencyProperty.Register("CQTY",
            typeof(double), typeof(UIFQI03),
            new FrameworkPropertyMetadata(0.0, FrameworkPropertyMetadataOptions.BindsTwoWayByDefault, OnCQTYChanged));
        public double CQTY
        {
            get
            {
                return (double)GetValue(CQTYProperty);
            }
            set
            {
                SetValue(CQTYProperty, value);
            }
        }
        private static void OnCQTYChanged(DependencyObject sender, DependencyPropertyChangedEventArgs e)
        {
            var src = sender as UIFQI03;
            if (src != null)
            {
                src.CQTY = (double)e.NewValue;
            }
        }
        #endregion
        TabItem cOnline
        {
            get
            {
                return (TabItem)this.FindName("cOnline");
            }
        }
        TFWB08 cWB08
        {
            get
            {
                return (TFWB08)this.FindName("cWB08");
            }
        }        
        public UIFQI03()
        {
            
        } 
     
        public override void vwMaster_CurrentChanged(object sender, EventArgs e)
        {
            base.vwMaster_CurrentChanged(sender, e);
            if (dwMaster == null)
            {
                RWAUF = "";
                ABAUF = ""; 
            }
            else
            {
                dwMaster["WBBDPART"] =  (PARTIE.PadRight(22)).Substring(0,11);
                dwMaster["WBBDPART2"] = (PARTIE.PadRight(22)).Substring(11).PadRight(11);
                dwMaster["WBBFILLER"] = DES.PadRight(60);
                RWAUF = TMV.WBBRWKRS + TMV.WBBRWLFD + TMV.WBBPOS;
                ABAUF = TMV.WBBAUFKRS + TMV.WBBAUFLFD + TMV.WBBAUFPOS;
                if (TMV.WBBWAKZ == "F")
                {
                    NSTKNR = TMV.WBBSTKNR;
                    cOnline.Focus();
                } 
            }
        }
        public override void Query_KeyUp(object sender, KeyEventArgs e)
        {
            base.Query_KeyUp(sender, e);
        }
        public override void QueryCommand(object sender, RoutedEventArgs e)
        {
            NSTKNR = "";
            CMTR = 0;
            CPCS = 0;
            IsEdit = true;
            IsSaved = false;
            if (STKNR == "")
            {
                INTEXVALIDATE();
                return;
            }
            PM["STKNR"] = STKNR;
            PM["ART"] = ART;
            PM["ABAUF"] = "";
            PM["LOOMID"] = LOOMID;
            PM["QITABLE"] = MACHINEID;
            SResult = TMV.QITXWBB(PM);
            MergeData(SResult, true);
            //base.QueryCommand(sender, e);
        }
       
        public override void NewCommand(object sender, RoutedEventArgs e)
        {
            IsBusy = true;
            IsAppend = false;
            IsAppend = true;
            //此时可以创建一个布卷，然后留位，然后选择布卷，然后开始验布
            CUTFCommand();
            base.NewCommand(sender, e);
            IsBusy = false;
        }
        public virtual void CutFCommand(object sender, RoutedEventArgs e)
        {
            Button btn = sender as Button;
            IsJP = true;
            if (btn.Tag.ToString().Contains("NP"))
            {
                IsJP = false;
            }
            IsBusy = true;
            IsPrint = false;
            IsPrint = true;
           
            CUTFCommand();            
        }
        public void CUTFCommand()
        {
           
            if (dwMaster == null || WFART.Length < 23)
            {
                MessageBox.Show("必须配合后整理生产单，否则不能确定成品属性");
                return;
            }
            if (QUALITY.Trim().Length == 0)
                QUALITY = "1A";
            if (dwMaster["ABFAFOLGE"].ToString().Trim().Length > 0)
            {
                dwMaster["WBBAFOLGEVON"] = dwMaster["ABFAFOLGE"];
            }
            //dwMaster["WBBWMNRKRS"] = MACHINEID.Substring(0, 2);
           // dwMaster["WBBWMLFDNR"] = MACHINEID.Substring(2, 4);
           // dwMaster["WBBWMPOS"] = MACHINEID.Substring(6, 2);
            TMV.WBBWMNRKRS = "M ";
            TMV.WBBWMLFDNR = Helper.Left(LOOMID.Trim().PadRight(4), 4);
            CPCS++;
            NSTKNR = "";
            //dwMaster["WBBISTKLG"] = 0;
            PM["dwWB"] = dwMaster;
            PM["CMTR"] = 0.0;
            PM["RUNID"] = RUNID;
            PM["WFART"] = WFART;
            PM["QIMACHINE"] = MACHINEID;
            PM["QUALITY"] = QUALITY;
            PM["WH2"] = WH;
            PM["FWIDTH"] = dwMaster["WAFBRTU"].ToString();
            PM["IsInsert"] = true;
            PM["SHIFT"] = SHIFT;
            PM["PARTIE"] = PARTIE;
            PM["ARTDES"] = DES;
            PM["LOOMID"] = LOOMID; 
            NSTKNR = TMV.CUTFWB(PM);
            TMV.VABP.ABFQTYPROD(dwMaster, "QTYMTR", "QI04".PadRight(8), MACHINEID, dwMaster["WBBTP"].ToString(), WFART, CMTR * 0.001);
            TMV.WBBINBSTMTR = TMV.WBBINBSTMTR + CMTR;
            CQTY = TMV.WBBINBSTMTR; 
            IsBusy = false;
            //vwMaster.View.MoveCurrentToFirst();
        }
        public void STKLG_LostFocus(object sender, RoutedEventArgs e)
        { 
            //ChgQtyCommand(null, null); 
        }
        public override void OnCMTRChg(double cmtr)
        {
            if (dwWB == null)
                return;
            if (RWAUF.Length == 0)
                return; 
            QTY = TMV.WBBINBSTMTR + cmtr;
            base.OnCMTRChg(cmtr);
        }  
        public override void OnSHIFTChg(string value)
        {
            base.OnSHIFTChg(value);
        }
        /// <summary>
        /// 1.好101-20米
        ///2.坏开始，101标记为待拼匹，写入数据库，102-10米
        ///3.坏结束，103标记为待拼匹
        ///4.循环 PB-200, CP-X -60,60, CP LB
        ///5.直到10X,为正常的完成按钮，搜索数据库找到待拼匹的布卷，修正10X的长度， 101-103....都减少数量，10X增加数量，按照拼匹次数增加。
        ///6.外面拼匹，使用相同功能，搜索条件外面设计
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        public void lbStartCommand(object sender, RoutedEventArgs e)
        {
            if(cWB08.QTY==0)
            {
                MessageBox.Show("请输入标签长度");
                return;
            }
            IsPrint = false;
            dwData = TBMain.NewRow();
            dwData["WBBMANDANT"] = app.Mandant;
            
            dwData["WBBSTKNR"] = NSTKNR;
            dwData["WBBTSTKLG"] = cWB08.CMTR;
            dwData["WBBISTKLG"] = cWB08.CMTR;
            dwData["WBBSSTKLG"] = cWB08.QTY;
            dwData["WBBSPSTA"] = "T";
            TMV.UWB22(dwData);
            string wh = WH;
            WH = "01LB01      ";
            CUTFCommand();
            WH = wh; 
        }
        public void lbEndCommand(object sender, RoutedEventArgs e)
        {
            IsPrint = false;
            CUTFCommand();
        }
    }
}
